package com.liukunup.reader;

import com.google.gson.Gson;

/**
 * 文本文件 JSON读取
 * @author Liu Kun
 * @date 2021-04-03 20:26:05
 **/
public class JsonReader<T> extends BaseTextReader {

    private final T clazz;

    public JsonReader(String filename, T clazz) {
        super(filename);
        this.clazz = clazz;
    }

    @Override
    public Object[] next() {
        // 存储返回数据
        Object[] objects = new Object[1];
        // 如果未读取到有效行则直接返回
        if (null == this.lineList) {
            return objects;
        }
        // 根据游标取数据
        String line = lineList.get(cursor);
        objects[0] = new Gson().fromJson(line, this.clazz.getClass());
        // 移动游标
        cursor += 1;
        // 返回数据
        return objects;
    }
}
